#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\math\optimization\endcriteria.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

#undef HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Math { namespace Optimization {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IEndCriteria
	public ref class CEndCriteria : 
            public System::MarshalByRefObject,
            public Cephei::QL::Math::Optimization::IEndCriteria
	{
	protected: 
		boost::shared_ptr<QuantLib::EndCriteria>* _ppEndCriteria;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::EndCriteria>* _phEndCriteria;
#endif
		Object^ _EndCriteriaOwner;     // reference to object that manages the storage for this object
	internal:
		CEndCriteria (UInt64 maxIterations, UInt64 maxStationaryStateIterations, Double rootEpsilon, Double functionEpsilon, Double gradientNormEpsilon);
        CEndCriteria (boost::shared_ptr<QuantLib::EndCriteria>& childNative, Object^ owner);
        CEndCriteria (QuantLib::EndCriteria& childNative, Object^ owner);
        CEndCriteria (CEndCriteria^ copy);
        CEndCriteria (System::Type^ t);
#ifdef STRUCT
        CEndCriteria (QuantLib::EndCriteria childNative);
#endif       
#ifdef HANDLE
		CEndCriteria (QuantLib::Handle<QuantLib::EndCriteria>& childNative, Object^ owner);
		CEndCriteria (QuantLib::Handle<QuantLib::EndCriteria> childNative);
#endif
		virtual ~CEndCriteria ();
		!CEndCriteria ();

	internal:
		QuantLib::EndCriteria& GetReference ();
		boost::shared_ptr<QuantLib::EndCriteria>& GetShared ();
		QuantLib::EndCriteria* GetPointer ();
        void SetEndCriteria (boost::shared_ptr<QuantLib::EndCriteria> native)
        {
            if (_ppEndCriteria != NULL)
                delete _ppEndCriteria;
            _ppEndCriteria = new boost::shared_ptr<QuantLib::EndCriteria> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::EndCriteria>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual Boolean CheckMaxIterations (UInt64 iteration, QL::Math::Optimization::EndCriteria::TypeEnum ecType) ;
		virtual Boolean CheckStationaryFunctionAccuracy (Double f, Boolean positiveOptimization, QL::Math::Optimization::EndCriteria::TypeEnum ecType) ;
		virtual Boolean CheckStationaryFunctionValue (Double fxOld, Double fxNew, UInt64 statStateIterations, QL::Math::Optimization::EndCriteria::TypeEnum ecType) ;
		virtual Boolean CheckStationaryPoint (Double xOld, Double xNew, UInt64 statStateIterations, QL::Math::Optimization::EndCriteria::TypeEnum ecType) ;
		virtual Boolean CheckZeroGradientNorm (Double gNorm, QL::Math::Optimization::EndCriteria::TypeEnum ecType) ;
        property Double FunctionEpsilon 
        {
		    virtual Double get () ;
        }
        property Double GradientNormEpsilon 
        {
		    virtual Double get () ;
        }
        property UInt64 MaxIterations 
        {
		    virtual UInt64 get () ;
        }
        property UInt64 MaxStationaryStateIterations 
        {
		    virtual UInt64 get () ;
        }
        property Double RootEpsilon 
        {
		    virtual Double get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CEndCriteria_Factory : public System::MarshalByRefObject,  public IEndCriteria_Factory
	{
	public:
        virtual IEndCriteria^ Create (UInt64 maxIterations, UInt64 maxStationaryStateIterations, Double rootEpsilon, Double functionEpsilon, Double gradientNormEpsilon);
    };
   
/*Cephei*/ } /*QL*/ } /*Math*/ } /*Optimization */}
